/*
* INSANE - Interactive Structural Analysis Environment
*
* Copyright (C) 2003-2005
* Universidade Federal de Minas Gerais
* Escola de Engenharia
* Departamento de Engenharia de Estruturas
* 
* Author's email :    insane@dees.ufmg.br
* Author's website :  http://www.dees.ufmg.br/insane
* 
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or any later version.
* 
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
* GNU General Public License for more details.
* 
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
*/
package br.ufmg.dees.insane.ui.rich.full.dialogs;

import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.Toolkit;
import java.text.DecimalFormat;
import java.text.NumberFormat;
import java.util.Locale;

import javax.swing.ImageIcon;
import javax.swing.JLabel;
import javax.swing.JPanel;

import br.ufmg.dees.insane.commons.dialogs.DecimalField;
import br.ufmg.dees.insane.commons.dialogs.SimpleDialog;
import br.ufmg.dees.insane.model.disc.node.Node;
import br.ufmg.dees.insane.ui.rich.full.InsaneInternalInterface;

/**
 * An abstract class extending JDialog representing a dialog on screen.
 *
 * @author Fonseca, Flavio & Pitangueira, Roque
 * @see gui.InsaneInternalInterface
 * @since April 2004
 */
public class NodeAngleDialog extends SimpleDialog {
	
	private static final long serialVersionUID = 1L;
	private InsaneInternalInterface parent;
	private NumberFormat numberFormat;
	
	private DecimalField angleField;
	public double angle;
	
//*****************************************************************************
	
	public NodeAngleDialog(InsaneInternalInterface parent) {
		this.parent = parent;
		setUpFormat();
		setUpFields();
		
		JPanel anglePane = new JPanel();
		anglePane.setPreferredSize(new Dimension(360, 280));
		anglePane.setLayout(new GridLayout(4,1,2,2));
		JPanel blankPane = new JPanel();
		anglePane.add(blankPane);
		JPanel anglePicPane = new JPanel();
		anglePicPane.setLayout(new GridLayout(1,1,2,2));
		anglePane.add(anglePicPane);
		JPanel angleValuePane = new JPanel();
		anglePane.add(angleValuePane);
		JPanel blankPane2 = new JPanel();
		anglePane.add(blankPane2);
		
		anglePicPane.add(new JLabel( new ImageIcon(ClassLoader.getSystemResource("br/ufmg/dees/insane/images/NodeAngle64.gif")) ));
		angleValuePane.add(new JLabel(parent.getResourceBundle().getString("dNAngDialog01")));
		angleValuePane.add(angleField);
		
		setPane(anglePane);
	}
	
//*****************************************************************************
	
    public int showDialog() {
    	return super.showDialog(parent, parent.getResourceBundle().getString("dNAngDialog02"));
    }
	
//*****************************************************************************
	
	/**
	 * This method is executed on OK button click.
	 */
	public void approvedPerform() {
		try {
			angle = angleField.getValue();
		} catch (Exception e) {
			Toolkit.getDefaultToolkit().beep();
			this.setVisible(false);
		}
	}
	
//*****************************************************************************
	/**
	 * Sets up the number formater
	 */
	private void setUpFormat() {
		numberFormat = (DecimalFormat)NumberFormat.getNumberInstance(Locale.UK);
		numberFormat.setMinimumFractionDigits(parent.getModelState().getPrecision());
		numberFormat.setMaximumFractionDigits(8);
	}
	
//*****************************************************************************
	
	/**
	 * Sets up fields
	 */
	private void setUpFields() {
		if (this.parent.getController().getSelection().size() == 1 ) {
			Node node = (Node)this.parent.getController().getSelection().get(0);
			angleField = new DecimalField(node.getAngle(), 7, numberFormat);
		} else {
			angleField = new DecimalField(0, 7, numberFormat);
		}
	}
	
//*****************************************************************************
}
